package dbbench

import (
	"math/rand"
	"strings"
	"time"
)

func randStr(r *rand.Rand, size int) string {
	sb := &strings.Builder{}
	for i := 0; i < size; i++ {
		sb.WriteByte(byte(i % 8))
	}
	return sb.String()
}

func randArrInt32(r *rand.Rand, size int) []int32 {
	list := make([]int32, size)
	for i := 0; i < size; i++ {
		list[i] = rand.Int31()
	}
	return list
}

func randArrStr(r *rand.Rand, size int) []string {
	list := make([]string, size)
	for i := 0; i < size; i++ {
		list[i] = randStr(r, rand.Intn(60))
	}
	return list
}

func RandData(r *rand.Rand) *P {
	return &P{
		I1:  r.Int31(),
		I2:  r.Int31(),
		I3:  r.Int31(),
		I4:  r.Int31(),
		I5:  r.Int31(),
		I6:  r.Int31(),
		I7:  r.Int31(),
		I8:  r.Int31(),
		I9:  r.Int31(),
		I10: r.Int31(),
		I11: r.Int31(),
		I12: r.Int31(),
		I13: r.Int31(),
		I14: r.Int31(),
		I15: r.Int31(),
		I16: r.Int31(),
		I17: r.Int31(),
		I18: r.Int31(),
		I19: r.Int31(),
		I20: r.Int31(),
		I21: randArrInt32(r, r.Intn(100)),
		I22: randArrInt32(r, r.Intn(100)),
		I23: randArrInt32(r, r.Intn(100)),
		I24: randArrInt32(r, r.Intn(100)),
		I25: randArrInt32(r, r.Intn(100)),
		I26: randArrInt32(r, r.Intn(100)),
		I27: randArrInt32(r, r.Intn(100)),
		I28: randArrInt32(r, r.Intn(100)),
		I29: randArrInt32(r, r.Intn(100)),
		I30: randArrInt32(r, r.Intn(100)),
		I31: randArrInt32(r, r.Intn(100)),
		I32: randArrInt32(r, r.Intn(100)),
		I33: randArrInt32(r, r.Intn(100)),
		I34: randArrInt32(r, r.Intn(100)),
		I35: randArrInt32(r, r.Intn(100)),
		I36: randArrInt32(r, r.Intn(100)),
		I37: randArrInt32(r, r.Intn(100)),
		I38: randArrInt32(r, r.Intn(100)),
		I39: randArrInt32(r, r.Intn(100)),
		I40: randArrInt32(r, r.Intn(100)),
		I41: randStr(r, r.Intn(60)),
		I42: randStr(r, r.Intn(60)),
		I43: randStr(r, r.Intn(60)),
		I44: randStr(r, r.Intn(60)),
		I45: randStr(r, r.Intn(60)),
		I46: randStr(r, r.Intn(60)),
		I47: randStr(r, r.Intn(60)),
		I48: randStr(r, r.Intn(60)),
		I49: randStr(r, r.Intn(60)),
		I50: randStr(r, r.Intn(60)),
		I51: randStr(r, r.Intn(60)),
		I52: randStr(r, r.Intn(60)),
		I53: randStr(r, r.Intn(60)),
		I54: randStr(r, r.Intn(60)),
		I55: randStr(r, r.Intn(60)),
		I56: randStr(r, r.Intn(60)),
		I57: randStr(r, r.Intn(60)),
		I58: randStr(r, r.Intn(60)),
		I59: randStr(r, r.Intn(60)),
		I60: randStr(r, r.Intn(60)),
		P1: &P1{
			I1:  randStr(r, r.Intn(60)),
			I2:  randStr(r, r.Intn(60)),
			I3:  randStr(r, r.Intn(60)),
			I4:  randStr(r, r.Intn(60)),
			I5:  randStr(r, r.Intn(60)),
			I6:  randStr(r, r.Intn(60)),
			I7:  randStr(r, r.Intn(60)),
			I8:  randStr(r, r.Intn(60)),
			I9:  randStr(r, r.Intn(60)),
			I10: randStr(r, r.Intn(60)),
		},
		P2: &P2{
			I1:  r.Int31(),
			I2:  r.Int31(),
			I3:  r.Int31(),
			I4:  r.Int31(),
			I5:  r.Int31(),
			I6:  r.Int31(),
			I7:  r.Int31(),
			I8:  r.Int31(),
			I9:  r.Int31(),
			I10: r.Int31(),
		},
	}
}

var cache []*P

func MakeCache(n int) {
	cache = make([]*P, n)
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < n; i++ {
		cache[i] = RandData(r)
	}
}
